//
//  QuestionAnswerVC.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/14.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "QuestionAnswerVC.h"
#import "QuestionAnswerTableViewCell.h"
#import "QuestionAnswer.h"
#import "AllCommentsViewController.h"
#import "QuestionAnswerApi.h"
#import "PersonalDetailsViewController.h"

@interface QuestionAnswerVC ()
{
    int   _typeInt;
}
@end

@implementation QuestionAnswerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _typeInt = 0;
    [self.tableView registerCellWithNib:@"QuestionAnswerTableViewCell"];
    [self configNotDataView:[UIImage imageNamed:@"noPingLun"] text:@"暂无问答"];
    
    [self addMj_footer];
    [self addMj_header];
    [self loadData];
    
    self.tableView.bounces = YES;
    self.navigationItem.rightBarButtonItem = [Tool BarButtonItemWithTitle:@"       筛选     " target:self action:@selector(itemAction:)];
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    if (!self.listApi) {
        self.listApi = [[QuestionAnswerApi alloc]init_TalkLists];
    }
    if ([self.listApi respondsToSelector:@selector(setTalkType:)]) {
        QuestionAnswerApi *api = (QuestionAnswerApi *)self.listApi;
        api.talkType = _typeInt;
    }
    WeakSelf(self)
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            for (NSDictionary *dic in [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"]) {
                QuestionAnswer *obj = [[QuestionAnswer alloc]init];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
            }
            [self showOrHideDataView];
        }
        [weakself.tableView reloadData];
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

-(void)itemAction:(UIButton *)btn
{
    NSArray *array = @[@"全部",@"最近1小时",@"过去3小时",@"过去12小时",@"最近一天"];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"筛选条件" message:nil preferredStyle:UIAlertControllerStyleAlert];
    for (int i = 0; i < 5; i++) {
        [alert addAction:[UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _typeInt = i;
            [btn setTitle:array[i] forState:UIControlStateNormal];
            [self loadData];
        }]];
    }
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuestionAnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionAnswerTableViewCell" forIndexPath:indexPath];
    
    QuestionAnswer *obj = self.dataArray[indexPath.row];
    [cell.questionAnswerView.image sd_setImageWithURL:[NSURL URLWithString:obj.user_avatar] placeholderImage:[UIImage imageNamed:@"AnonymousAvatar"]];
    cell.questionAnswerView.questionTitle.text = obj.title;
    cell.questionAnswerView.questionContent.attributedText = obj.content;
    cell.questionAnswerView.time.text = obj.created_at;
    cell.questionAnswerView.name.text = obj.user_nickname;
    
    WeakSelf(obj)
    WeakSelf(self)
    cell.replyAction = ^(id obj) {
        weakobj.isPopUpKeyboard = YES;
        [weakself tableView:tableView didSelectRowAtIndexPath:indexPath];
    };
    
    cell.questionAnswerView.clickImageAction = ^(id obj) {
        [weakself pushViewController:[PersonalDetailsViewController PersonalDetailsViewController:nil UserID:weakobj.user_id] animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuestionAnswer *obj = self.dataArray[indexPath.row];
    return [obj text_H] + 75.5/*内容label的y*/ + 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllCommentsViewController *vc = [[AllCommentsViewController alloc]init];
    vc.questionAnswer = self.dataArray[indexPath.row];
    [self pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
